//
//  SISSPassengerRegistSuccessViewController.m
//  SISSAPP
//
//  Created by 袁应荣 on 2019/10/31.
//  Copyright © 2019 优房网. All rights reserved.
//

#import "SISSPassengerRegistSuccessViewController.h"
#import "SISSHousingCell.h"
#import "SISSJsonRequestNetworkingManager+Resources.h"
#import "SISSJsonRequestNetworkingManager+Registration.h"
#import "SISSRecommendPassengerViewController.h"

@interface SISSPassengerRegistSuccessViewController ()<SISSHousingCellDelegate>

@property (copy, nonatomic) NSArray<SISSHouseResourceModel *> *houseResourceArr;

@end

@implementation SISSPassengerRegistSuccessViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"客源登记成功";
    
    self.tipStr = @"以下房源和该客户的需求匹配度很高，您可以点击推荐客户给下面房源的业主。";
    [self.tableView registerNib:[UINib nibWithNibName:@"SISSHousingCell" bundle:nil] forCellReuseIdentifier:@"houseCellReuseID"];
//    [self requestData];
}

- (void)backReccommend {
    for (UIViewController *controller in self.navigationController.viewControllers) {
        if ([controller isKindOfClass:[SISSRecommendPassengerViewController class]]) {
            [self.navigationController popToViewController:controller animated:YES];
        }
    }
}

- (void)setAbdId:(NSString *)abdId {
    _abdId = abdId;
    [self requestData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.houseResourceArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    SISSHousingCell *cell = [tableView dequeueReusableCellWithIdentifier:@"houseCellReuseID" forIndexPath:indexPath];
    cell.delegate = self;
    
    cell.houseResourceModel = self.houseResourceArr[indexPath.row];
    
    return cell;
}

#pragma mark - SISSHousingCellDelegate
- (void)housingCellRecommendButtonDidClick:(UIButton *)button {
    
    SISSHousingCell *cell = (SISSHousingCell *)button.nextResponder.nextResponder;
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    SISSHouseResourceModel *model = self.houseResourceArr[indexPath.row];
    SISSPassengerResourceModel *passengerModel = [[SISSPassengerResourceModel alloc] init];
    passengerModel.demandType = 2;
    passengerModel.ID = self.abdId;
    passengerModel.isSelected = YES;
    [kJsonRequestNetworkingManager requestInsetRecommendPassengerWithHouseId:model.ID passengerArr:@[passengerModel] success:^(id responseObject) {
        
        if ([responseObject[@"code"] integerValue] == 200) {
            NSLog(@"%@",responseObject[@"message"]);
            
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                
                [SISSTools showSVSuccessTitle:@"推荐成功" andTime:2 andColor:YES];
            }];
            [self requestData];
        }
        
    } failed:^(NSError *error) {
        
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            
            [SISSTools showSVErrorTitle:@"推荐失败" andTime:2 andColor:YES];
        }];
        NSLog(@"%@",error);
    }];
}

- (void)requestData {
    
    [kJsonRequestNetworkingManager requestBuyerDemandRecommendHouseWithAbdId:self.abdId success:^(NSArray<SISSHouseResourceModel *> *houseResources) {
        
        self.houseResourceArr = houseResources;
        [self.tableView reloadData];
        
    } failed:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}

@end
